﻿using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using StockTrader.DAL.IDAL;
using StockTrader.DAL.MySQLHelper;
using StockTrader.BusinessService.DataContract;

namespace StockTrader.DAL.MySQL
{
    public class DailySummary : IDailySummary
    {
        private MySqlConnection _internalConnection;
        private MySqlTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new MySqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }
        #region SqlStr
        private const string INSERT_DAILYSUMMARY = "INSERT INTO DAILYSUMMARY(transdate, symbol, openPrice, highPrice, lowPrice, closePrice, averagePrice, total, p1, p1value, p2, p2value, p3, p3value, buyorder, sellorder, buycount, sellcount, pt, ptvalue, listing, inmarket, eps, adjustedeps, benefit, foreignown, stateown, market)  VALUES (@transdate, @symbol, @open, @high, @low, @close, @average, @total, @p1, @p1value, @p2, @p2value, @p3, @p3value, @buyorder, @sellorder, @buycount, @sellcount, @pt, @ptvalue, @listing, @inmarket, @eps, @adjustedeps, @benefit, @foreignown, @stateown, @market) ON DUPLICATE KEY UPDATE transdate= @transdate, symbol=@symbol, openPrice=@open, highPrice=@high, lowPrice=@low, closePrice=@close, averagePrice=@average, total=@total, p1=@p1, p2=@p2, p3=@p3, p1value=@p1value, p2value=@p2value, p3value=@p3value, buyorder=@buyorder, sellorder=@sellorder, buycount=@buycount, sellcount=@sellcount, pt=@pt, ptvalue=@ptvalue, listing=@listing, inmarket=@inmarket, eps=@eps, adjustedeps=@adjustedeps, benefit=@benefit, foreignown=@foreignown, stateown=@stateown, market=@market";

        private const string INSERT_FOREIGNTRADE = "INSERT INTO DAILYFOREIGNTRADE(transdate, symbol, buy, buyvalue, sell, sellvalue, buyp1, buyp2, buyp3, sellp1, sellp2, sellp3, ptbuy, ptbuyvalue, ptsell, ptsellvalue, room, remain) VALUES (@transdate, @symbol, @buy, @buyvalue, @sell, @sellvalue, @buyp1, @buyp2, @buyp3, @sellp1, @sellp2, @sellp3, @ptbuy, @ptbuyvalue, @ptsell, @ptsellvalue, @room, @remain) ON DUPLICATE KEY UPDATE transdate=@transdate, symbol=@symbol, buy=@buy, buyvalue=@buyvalue, sell=@sell, sellvalue=@sellvalue, buyp1=@buyp1, buyp2=@buyp2, buyp3=@buyp3, sellp1=@sellp1, sellp2=@sellp2, sellp3=@sellp3, ptbuy=@ptbuy, ptbuyvalue=@ptbuyvalue, ptsellvalue=@ptsellvalue, ptsell=@ptsell, room=@room, remain=@remain";

        private const string INSERT_FUNDTRADE = "INSERT INTO DAILYFUNDTRADE(transdate, symbol, side, register, inday, indayRatio, accumulate, accumulateRatio, remain, remainRatio, registerDate) VALUES (@transdate, @symbol, @side, @register, @inday, @indayRatio, @accumulate, @accumulateRatio, @remain, @remainRatio, @registerDate) ON DUPLICATE KEY UPDATE transdate=@transdate, symbol=@symbol,side=@side, register=@register, inday=@inday,indayRatio=@indayRatio,accumulate=@accumulate,accumulateRatio=@accumulateRatio,remain=@remain, remainRatio=@remainRatio, registerDate=@registerDate";
        #endregion

        #region
        private const string PARM_TRANSDATE = "@TRANSDATE";
        private const String PARM_SYMBOL = "@SYMBOL";
        #endregion

        #region IDailySummary Members

        public void InsertDailySummary(DateTime transdate, string symbol, int open, int high, int low, int close, float average, int total, int p1, long p1value, int p2, long p2value, int p3, long p3value, int buyorder, int sellorder, int buycount, int sellcount, int pt, int ptvalue, long listing, long inmarket, float eps, float adjustedeps, int benefit, float foreignown, float stateown, string market)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                                                new MySqlParameter(PARM_TRANSDATE, MySqlDbType.DateTime),
                                                new MySqlParameter(PARM_SYMBOL, MySqlDbType.VarChar, 8), 
                                                new MySqlParameter("@Open", MySqlDbType.Int32),
                                                new MySqlParameter("@High", MySqlDbType.Int32),
                                                new MySqlParameter("@Low", MySqlDbType.Int32),
                                                new MySqlParameter("@Close", MySqlDbType.Int32),
                                                new MySqlParameter("@Average", MySqlDbType.Float),
                                                new MySqlParameter("@total", MySqlDbType.Int32),
                                                new MySqlParameter("@p1", MySqlDbType.Int32),
                                                new MySqlParameter("@p1value", MySqlDbType.Int64),
                                                new MySqlParameter("@p2", MySqlDbType.Int32),
                                                new MySqlParameter("@p2value", MySqlDbType.Int64),
                                                new MySqlParameter("@p3", MySqlDbType.Int32),
                                                new MySqlParameter("@p3value", MySqlDbType.Int64),
                                                new MySqlParameter("@buyorder", MySqlDbType.Int32),
                                                new MySqlParameter("@sellorder", MySqlDbType.Int32),
                                                new MySqlParameter("@buycount", MySqlDbType.Int32),
                                                new MySqlParameter("@sellcount", MySqlDbType.Int32),
                                                new MySqlParameter("@pt", MySqlDbType.Int32),
                                                new MySqlParameter("@ptvalue", MySqlDbType.Int32),
                                                new MySqlParameter("@listing", MySqlDbType.Int64),
                                                new MySqlParameter("@inmarket", MySqlDbType.Int64),
                                                new MySqlParameter("@eps", MySqlDbType.Float),
                                                new MySqlParameter("@adjustedeps", MySqlDbType.Float),
                                                new MySqlParameter("@benefit", MySqlDbType.Int32),
                                                new MySqlParameter("@foreignown", MySqlDbType.Float),
                                                new MySqlParameter("@stateown", MySqlDbType.Float),
                                                new MySqlParameter("@market", MySqlDbType.VarChar)
                };
                parms[0].Value = transdate;
                parms[1].Value = symbol;
                parms[2].Value = open;
                parms[3].Value = high;
                parms[4].Value = low;
                parms[5].Value = close;
                parms[6].Value = average;
                parms[7].Value = total;
                parms[8].Value = p1;
                parms[9].Value = p1value;
                parms[10].Value = p2;
                parms[11].Value = p2value;
                parms[12].Value = p3;
                parms[13].Value = p3value;
                parms[14].Value = buyorder;
                parms[15].Value = sellorder;
                parms[16].Value = buycount;
                parms[17].Value = sellcount;
                parms[18].Value = pt;
                parms[19].Value = ptvalue;
                parms[20].Value = listing;
                parms[21].Value = inmarket;
                parms[22].Value = eps;
                parms[23].Value = adjustedeps;
                parms[24].Value = benefit;
                parms[25].Value = foreignown;
                parms[26].Value = stateown;
                parms[27].Value = market;


                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_DAILYSUMMARY, parms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void InsertDailyForeignTrade(DateTime transdate, string symbol, int buy, long buyvalue, int sell, long sellvalue, int p1buy, int p2buy, int p3buy, int p1sell, int p2sell, int p3sell, int ptbuy, long ptbuyvalue, int ptsell, long ptsellvalue, long room, long remain)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                                                new MySqlParameter(PARM_TRANSDATE, MySqlDbType.DateTime),
                                                new MySqlParameter(PARM_SYMBOL, MySqlDbType.VarChar, 8), 
                                                new MySqlParameter("@buy", MySqlDbType.Int32),
                                                new MySqlParameter("@buyvalue", MySqlDbType.Int64),
                                                new MySqlParameter("@sell", MySqlDbType.Int32),
                                                new MySqlParameter("@sellvalue", MySqlDbType.Int64),
                                                new MySqlParameter("@buyp1", MySqlDbType.Int32),
                                                new MySqlParameter("@buyp2", MySqlDbType.Int32),
                                                new MySqlParameter("@buyp3", MySqlDbType.Float),
                                                new MySqlParameter("@sellp1", MySqlDbType.Int32),
                                                new MySqlParameter("@sellp2", MySqlDbType.Int32),
                                                new MySqlParameter("@sellp3", MySqlDbType.Int32),
                                                new MySqlParameter("@ptbuy", MySqlDbType.Int32),
                                                new MySqlParameter("@ptbuyvalue", MySqlDbType.Int64),
                                                new MySqlParameter("@ptsell", MySqlDbType.Int32),
                                                new MySqlParameter("@ptsellvalue", MySqlDbType.Int64),
                                                new MySqlParameter("@room", MySqlDbType.Int64),
                                                new MySqlParameter("@remain", MySqlDbType.Int64) 
                };
                parms[0].Value = transdate;
                parms[1].Value = symbol;
                parms[2].Value = buy;
                parms[3].Value = buyvalue;
                parms[4].Value = sell;
                parms[5].Value = sellvalue;
                parms[6].Value = p1buy;
                parms[7].Value = p2buy;
                parms[8].Value = p3buy;
                parms[9].Value = p1sell;
                parms[10].Value = p2sell;
                parms[11].Value = p3sell;
                parms[12].Value = ptbuy;
                parms[13].Value = ptbuyvalue;
                parms[14].Value = ptsell;
                parms[15].Value = ptsellvalue;
                parms[16].Value = room;
                parms[17].Value = remain;

                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_FOREIGNTRADE, parms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void InsertDailyFundTrade(DateTime transdate, string symbol, string side, long register, int inday, float indayRatio,  int accumulate, float accumulateRatio, int remain, float remainRatio, DateTime registerdate)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                                                new MySqlParameter(PARM_TRANSDATE, MySqlDbType.DateTime),
                                                new MySqlParameter(PARM_SYMBOL, MySqlDbType.VarChar, 8), 
                                                new MySqlParameter("@side", MySqlDbType.VarChar, 1),
                                                new MySqlParameter("@register", MySqlDbType.Int64, 1),
                                                new MySqlParameter("@inday", MySqlDbType.Int32),
                                                new MySqlParameter("@indayRatio", MySqlDbType.Float),
                                                new MySqlParameter("@accumulate", MySqlDbType.Int32),
                                                new MySqlParameter("@accumulateRatio", MySqlDbType.Float),
                                                new MySqlParameter("@remain", MySqlDbType.Int32),
                                                new MySqlParameter("@remainRatio", MySqlDbType.Float),
                                                new MySqlParameter("@registerdate", MySqlDbType.Date),

                };
                parms[0].Value = transdate;
                parms[1].Value = symbol;
                parms[2].Value = side;
                parms[3].Value = register;
                parms[4].Value = inday;
                parms[5].Value = indayRatio;
                parms[6].Value = accumulate;
                parms[7].Value = accumulateRatio;
                parms[8].Value = remain;
                parms[9].Value = remainRatio;
                parms[10].Value = registerdate;

                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_FUNDTRADE, parms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

    }
}
